<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :showFooter="true"
    :okAuth="'physical:physicalSkillsScore:edit'"
    title="等级规则"
    width="60%"
    @ok="handleSubmit"
  >
    <BasicTable @register="registerTable" />
  </BasicDrawer>
</template>

<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightTable',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import {
    PhysicalSkillsScore,
    physicalSkillsScoreSelectGradeLevel,
    physicalSkillsScoreSaveGradeLevel,
  } from '/@/api/physical/physicalSkillsScore';
  const { showMessage } = useMessage();
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<PhysicalSkillsScore>({} as PhysicalSkillsScore);
  const tableColumns: BasicColumn[] = [
    {
      title: t('指标名称'),
      dataIndex: 'termName',
      key: 'a.index_name',
      //sorter: true,
      align: 'left',
      dictType: 's_index_type',
      slots: { customRender: 'firstColumn' },
    },
    {
      title: t('单项得分'),
      dataIndex: 'singleScore',
      key: 'a.singleScore',
      //sorter: true,
      align: 'center',
    },
    {
      title: t('等级'),
      dataIndex: 'scoreGrade',
      key: 'a.score_grade',
      //sorter: true,
      align: 'left',
      dictType: 's_term_score_grade',
    },
  ];

  let dataTable;
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner((data) => {
    console.log(data);
    dataTable = data;
    setSelectedRowKeys([data.scoreGradeId]);
    reload();
  });
  const [
    registerTable,
    { reload, getSelectRows, clearSelectedRowKeys, setSelectedRowKeys, getSelectRowKeys },
  ] = useTable({
    api: async () => {
      const res = await physicalSkillsScoreSelectGradeLevel(dataTable);
      return res.data;
    },
    beforeFetch: (params) => {
      return params;
    },
    rowSelection:{type:"radio"},
    clickToRowSelect:true,
    columns: tableColumns,
    showTableSetting: false,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openModal(true, record);
  }
  async function handleSubmit() {
    try {
      let scoreGradeId = getSelectRows();
      let data = {
        scoreGradeId: scoreGradeId[0].scoreGradeId,
        scoreId: dataTable.scoreId,
      };
      let res = await physicalSkillsScoreSaveGradeLevel(data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      clearSelectedRowKeys();
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    }
  }
  function handleSuccess() {
    reload();
  }
</script>
